<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function ListNode(val, next) {
        this.val = val === undefined ? 0 : val;
        this.next = next === undefined ? null : next;
      }
      //1 --> 2 --> 3
      var reverseList = function (head) {
        if (!head) return head;
        //因为反转 需要最后一个是null，即代表开头pre要为null。所以需要虚拟头节点
        let dummy = new ListNode(null);
        let pre = dummy;
        let cur = head;
        let next = null;
        while (cur) {
          next = cur.next;
          cur.next = pre;
          pre = cur;
          cur = next;
        }
        //最后会多出一个值为null的空节点，需要去除。head就是尾节点
        head.next = null;
        return pre;
      };
    </script>
  </body>
</html>
